Add GtkLayoutManager
authorEmmanuele Bassi <ebassi@gnome.org>
Wed, 12 Dec 2018 15:24:41 +0000 (15:24 +0000)
committerEmmanuele Bassi <ebassi@gnome.org>
Tue, 26 Mar 2019 00:11:27 +0000 (00:11 +0000)
A base abstract class for layout manager delegate objects.

Layout managers are associated to a single widget, like event
controllers, and are responsible for measuring and allocating the
children of the widget they are bound to.

docs/reference/gtk/gtk4-sections.txt
gtk/gtk.h
gtk/gtklayoutmanager.c [new file with mode: 0644]
gtk/gtklayoutmanager.h [new file with mode: 0644]
gtk/gtklayoutmanagerprivate.h [new file with mode: 0644]
gtk/meson.build

index 5eb547bf6170a1555e4d49e8979b4ce5af6a29c0..8ce5bb1d8288dc416377028af62b886735222491 100644 (file)
@@ -7152,3 +7152,19 @@ gtk_root_get_for_surface
 <SUBSECTION Private>
 gtk_root_get_type
 </SECTION>
+
+<SECTION>
+<FILE>gtklayoutmanager</FILE>
+GtkLayoutManager
+GtkLayoutManagerClass
+
+gtk_layout_manager_measure
+gtk_layout_manager_allocate
+gtk_layout_manager_get_request_mode
+gtk_layout_manager_get_widget
+gtk_layout_manager_layout_changed
+
+<SUBSECTION Standard>
+GTK_TYPE_LAYOUT_MANAGER
+gtk_layout_manager_get_type
+</SECTION>
index 34fff24a3f76398bf55bc720bc23e4d99ff95d22..d6e419a55b8ed12d89d641ddf497b1d1b2be06d0 100644 (file)
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
 #include <gtk/gtkinfobar.h>
 #include <gtk/gtklabel.h>
 #include <gtk/gtklayout.h>
+#include <gtk/gtklayoutmanager.h>
 #include <gtk/gtklevelbar.h>
 #include <gtk/gtklinkbutton.h>
 #include <gtk/gtklistbox.h>
diff --git a/gtk/gtklayoutmanager.c b/gtk/gtklayoutmanager.c
new file mode 100644 (file)
index 0000000..bb23c5a
--- /dev/null
@@ -0,0 +1,222 @@
+/* gtklayoutmanager.c: Layout manager base class
+ * Copyright 2018  The GNOME Foundation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Emmanuele Bassi
+ */
+
+/**
+ * SECTION:gtklayoutmanager
+ * @Title: GtkLayoutManager
+ * @Short_description: Base class for layout manager
+ *
+ * ...
+ */
+
+#include "config.h"
+
+#include "gtklayoutmanager.h"
+
+#ifdef G_ENABLE_DEBUG
+#define LAYOUT_MANAGER_WARN_NOT_IMPLEMENTED(m,method)   G_STMT_START {  \
+        GObject *_obj = G_OBJECT (m);                                   \
+        g_warning ("Layout managers of type %s do not implement "       \
+                   "the GtkLayoutManager::%s method",                   \
+                   G_OBJECT_TYPE_NAME (_obj),                           \
+                   #method);                           } G_STMT_END
+#else
+#define LAYOUT_MANAGER_WARN_NOT_IMPLEMENTED(m,method)
+#endif
+
+typedef struct {
+  GtkWidget *widget;
+} GtkLayoutManagerPrivate;
+
+G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GtkLayoutManager, gtk_layout_manager, G_TYPE_OBJECT)
+
+static GtkSizeRequestMode
+gtk_layout_manager_real_get_request_mode (GtkLayoutManager *manager,
+                                          GtkWidget        *widget)
+{
+  LAYOUT_MANAGER_WARN_NOT_IMPLEMENTED (manager, get_request_mode);
+
+  return GTK_SIZE_REQUEST_CONSTANT_SIZE;
+}
+
+static void
+gtk_layout_manager_real_measure (GtkLayoutManager *manager,
+                                 GtkWidget        *widget,
+                                 GtkOrientation    orientation,
+                                 int               for_size,
+                                 int              *minimum,
+                                 int              *natural,
+                                 int              *baseline_minimum,
+                                 int              *baseline_natural)
+{
+  LAYOUT_MANAGER_WARN_NOT_IMPLEMENTED (manager, measure);
+
+  if (minimum != NULL)
+    *minimum = 0;
+
+  if (natural != NULL)
+    *natural = 0;
+
+  if (baseline_minimum != NULL)
+    *baseline_minimum = 0;
+
+  if (baseline_natural != NULL)
+    *baseline_natural = 0;
+}
+
+static void
+gtk_layout_manager_real_allocate (GtkLayoutManager *manager,
+                                  GtkWidget        *widget,
+                                  int               width,
+                                  int               height,
+                                  int               baseline)
+{
+  LAYOUT_MANAGER_WARN_NOT_IMPLEMENTED (manager, allocate);
+}
+
+static void
+gtk_layout_manager_class_init (GtkLayoutManagerClass *klass)
+{
+  klass->get_request_mode = gtk_layout_manager_real_get_request_mode;
+  klass->measure = gtk_layout_manager_real_measure;
+  klass->allocate = gtk_layout_manager_real_allocate;
+}
+
+static void
+gtk_layout_manager_init (GtkLayoutManager *self)
+{
+}
+
+/*< private >
+ * gtk_layout_manager_set_widget:
+ * @layout_manager: a #GtkLayoutManager
+ * @widget: (nullable): a #GtkWidget
+ *
+ * ...
+ */
+void
+gtk_layout_manager_set_widget (GtkLayoutManager *layout_manager,
+                               GtkWidget        *widget)
+{
+  GtkLayoutManagerPrivate *priv = gtk_layout_manager_get_instance_private (layout_manager);
+
+  priv->widget = widget;
+}
+
+/**
+ * gtk_layout_manager_measure:
+ * @manager:
+ * @widget:
+ * @orientation:
+ * @for_size:
+ * @minimum: (out):
+ * @natural: (out):
+ * @minimum_baseline: (out):
+ * @natural_baseline: (out):
+ *
+ * ...
+ *
+ */
+void
+gtk_layout_manager_measure (GtkLayoutManager *manager,
+                            GtkWidget        *widget,
+                            GtkOrientation    orientation,
+                            int               for_size,
+                            int              *minimum,
+                            int              *natural,
+                            int              *minimum_baseline,
+                            int              *natural_baseline)
+{
+  GtkLayoutManagerClass *klass;
+
+  g_return_if_fail (GTK_IS_LAYOUT_MANAGER (manager));
+  g_return_if_fail (GTK_IS_WIDGET (widget));
+
+  klass = GTK_LAYOUT_MANAGER_GET_CLASS (manager);
+
+  klass->measure (manager, widget, orientation,
+                  for_size,
+                  minimum, natural,
+                  minimum_baseline, natural_baseline);
+}
+
+/**
+ * gtk_layout_manager_allocate:
+ * @manager:
+ * @widget:
+ * @width:
+ * @height:
+ * @baseline:
+ *
+ * ...
+ */
+void
+gtk_layout_manager_allocate (GtkLayoutManager *manager,
+                             GtkWidget        *widget,
+                             int               width,
+                             int               height,
+                             int               baseline)
+{
+  GtkLayoutManagerClass *klass;
+
+  g_return_if_fail (GTK_IS_LAYOUT_MANAGER (manager));
+  g_return_if_fail (GTK_IS_WIDGET (widget));
+
+  klass = GTK_LAYOUT_MANAGER_GET_CLASS (manager);
+
+  klass->allocate (manager, widget, width, height, baseline);
+}
+
+/**
+ * gtk_layout_manager_get_request_mode:
+ * @manager:
+ * @widget:
+ *
+ * ...
+ *
+ * Returns: ...
+ */
+GtkSizeRequestMode
+gtk_layout_manager_get_request_mode (GtkLayoutManager *manager,
+                                     GtkWidget        *widget)
+{
+  GtkLayoutManagerClass *klass;
+
+  g_return_val_if_fail (GTK_IS_LAYOUT_MANAGER (manager), GTK_SIZE_REQUEST_CONSTANT_SIZE);
+  g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_SIZE_REQUEST_CONSTANT_SIZE);
+
+  klass = GTK_LAYOUT_MANAGER_GET_CLASS (manager);
+
+  return klass->get_request_mode (manager, widget);
+}
+
+/**
+ * gtk_layout_manager_layout_changed:
+ * @manager: a #GtkLayoutManager
+ *
+ * ...
+ */
+void
+gtk_layout_manager_layout_changed (GtkLayoutManager *manager)
+{
+  GtkLayoutManagerPrivate *priv = gtk_layout_manager_get_instance_private (manager);
+
+  if (priv->widget != NULL)
+    gtk_widget_queue_resize (priv->widget);
+}
diff --git a/gtk/gtklayoutmanager.h b/gtk/gtklayoutmanager.h
new file mode 100644 (file)
index 0000000..e849e30
--- /dev/null
@@ -0,0 +1,94 @@
+/* gtklayoutmanager.h: Layout manager base class
+ * Copyright 2018  The GNOME Foundation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Emmanuele Bassi
+ */
+#pragma once
+
+#include <gtk/gtkcontainer.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_LAYOUT_MANAGER (gtk_layout_manager_get_type ())
+
+GDK_AVAILABLE_IN_ALL
+G_DECLARE_DERIVABLE_TYPE (GtkLayoutManager, gtk_layout_manager, GTK, LAYOUT_MANAGER, GObject)
+
+/**
+ * GtkLayoutManagerClass:
+ * @get_request_mode: a virtual function, used to return the preferred
+ *   request mode for the layout manager; for instance, "width for height"
+ *   or "height for width"; see #GtkSizeRequestMode
+ * @measure: a virtual function, used to measure the minimum and preferred
+ *   sizes of the widget using the layout manager for a given orientation
+ * @allocate: a virtual function, used to allocate the size of the widget
+ *   using the layout manager
+ *
+ * The `GtkLayoutManagerClass` structure contains only private data, and
+ * should only be accessed through the provided API, or when subclassing
+ * #GtkLayoutManager.
+ */
+struct _GtkLayoutManagerClass
+{
+  /*< private >*/
+  GObjectClass parent_class;
+
+  /*< public >*/
+  GtkSizeRequestMode (* get_request_mode) (GtkLayoutManager *manager,
+                                           GtkWidget        *widget);
+
+  void               (* measure)          (GtkLayoutManager *manager,
+                                           GtkWidget        *widget,
+                                           GtkOrientation    orientation,
+                                           int               for_size,
+                                           int              *minimum,
+                                           int              *natural,
+                                           int              *minimum_baseline,
+                                           int              *natural_baseline);
+
+  void               (* allocate)         (GtkLayoutManager *manager,
+                                           GtkWidget        *widget,
+                                           int               width,
+                                           int               height,
+                                           int               baseline);
+
+  /*< private >*/
+  gpointer _padding[16];
+};
+
+GDK_AVAILABLE_IN_ALL
+void                    gtk_layout_manager_measure              (GtkLayoutManager *manager,
+                                                                 GtkWidget        *widget,
+                                                                 GtkOrientation    orientation,
+                                                                 int               for_size,
+                                                                 int              *minimum,
+                                                                 int              *natural,
+                                                                 int              *minimum_baseline,
+                                                                 int              *natural_baseline);
+GDK_AVAILABLE_IN_ALL
+void                    gtk_layout_manager_allocate             (GtkLayoutManager *manager,
+                                                                 GtkWidget        *widget,
+                                                                 int               width,
+                                                                 int               height,
+                                                                 int               baseline);
+GDK_AVAILABLE_IN_ALL
+GtkSizeRequestMode      gtk_layout_manager_get_request_mode     (GtkLayoutManager *manager,
+                                                                 GtkWidget        *widget);
+
+GDK_AVAILABLE_IN_ALL
+void                    gtk_layout_manager_layout_changed       (GtkLayoutManager *manager);
+
+G_END_DECLS
diff --git a/gtk/gtklayoutmanagerprivate.h b/gtk/gtklayoutmanagerprivate.h
new file mode 100644 (file)
index 0000000..d02ed56
--- /dev/null
@@ -0,0 +1,10 @@
+#pragma once
+
+#include "gtklayoutmanager.h"
+
+G_BEGIN_DECLS
+
+void gtk_layout_manager_set_widget (GtkLayoutManager *manager,
+                                    GtkWidget        *widget);
+
+G_END_DECLS
index ef9b7a0e32d4f5e67649105b953c224592abfc76..ce3147e5afed7e5e473ef7498981d13d35cfef10 100644 (file)
@@ -256,6 +256,7 @@ gtk_public_sources = files([
   'gtkinfobar.c',
   'gtklabel.c',
   'gtklayout.c',
+  'gtklayoutmanager.c',
   'gtklevelbar.c',
   'gtklinkbutton.c',
   'gtklistbox.c',
@@ -506,6 +507,7 @@ gtk_public_headers = files([
   'gtkinfobar.h',
   'gtklabel.h',
   'gtklayout.h',
+  'gtklayoutmanager.h',
   'gtklevelbar.h',
   'gtklinkbutton.h',
   'gtklistbox.h',